package org.example.controller.post;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Param;
import org.example.annotation.HasRole;
import org.example.dao.commom.Result;
import org.example.dao.commom.ResultCode;
import org.example.dao.dto.post.PReplyDto;
import org.example.dao.entity.post.PComment;
import org.example.dao.entity.post.PReply;
import org.example.service.post.ReplyService;
import org.example.skytool.commom.ParamUtil;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 22449
 */
@RestController
@RequestMapping("/post")
@CrossOrigin
public class ReplyController {


    //TODO 功能

    /**
     * 点赞回复(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("likeReply")
    public Result likeReply(@RequestBody PReply tReply) {
        Long id = tReply.getId();

        ParamUtil.LongParam(id);

        Long tReply1 = replyService.likeReply(id);
        return new Result(ResultCode.SUCCESS, tReply1);
    }

    //TODO 新增

    /**
     * 添加回复(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("replyComment")
    public Result replyComment(@RequestBody PReply tReply) {
        Long receiverId = tReply.getReceiverId();
        Long commentId = tReply.getCommentId();
        String content = tReply.getContent();

        ParamUtil.LongParam(receiverId,commentId);
        ParamUtil.StringParam(content);

        PReplyDto tReply1 = replyService.replyComment(receiverId, commentId, content);
        return new Result(ResultCode.SUCCESS, tReply1);
    }

    //TODO 查询

    /**
     * 获取对我的回复(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("getReplyByToMine/{limit}/{start}")
    public Result getReplyByToMine(@RequestBody PComment pComment,
                                     @PathVariable("limit")Long limit,
                                     @PathVariable("start")Long start){
        Integer isread =pComment.getIsRead();
        List<PReplyDto> list = replyService.getReplyByToMine(isread,limit,start);
        return new Result(ResultCode.SUCCESS,list);
    }





    /**
     * 获取回复(用户)
     */
    @HasRole(role = "用户")
    @PostMapping("getReplyByComment/{limit}/{start}")
    public Result getReplyByComment(@RequestBody PComment tComment,
                                    @PathVariable("limit") Integer limit,
                                    @PathVariable("start") Integer start) {
        Long commentId = tComment.getId();

        ParamUtil.LongParam(commentId);

        IPage<PReplyDto> list = replyService.getReplyByComment(commentId, limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    /**
     * 获取本人回复列表(用户)
     */
    @PostMapping("getReplyByMine/{limit}/{start}")
    public Result getReplyByMine(@PathVariable("limit")Long limit,
                                 @PathVariable("start")Long start){
        IPage<PReplyDto> list = replyService.getReplyByMine(limit, start);
        return new Result(ResultCode.SUCCESS, list);
    }

    //TODO 更新

    /**
     * 修改回复(用户)
     */
    @PostMapping("updateReply")
    public Result updateReply(@RequestBody PReply pReply){
        Long id = pReply.getId();
        String content = pReply.getContent();

        ParamUtil.LongParam(id);
        ParamUtil.StringParam(content);

        PReplyDto pReplyDto = replyService.updateReply(id,content);
        return new Result(ResultCode.SUCCESS,pReplyDto);
    }

    //TODO 删除

    /**
     * 删除回复(用户)
     */
    @PostMapping("deleteReply")
    public Result deleteReply(@RequestBody PReply pReply){
        Long id  = pReply.getId();

        ParamUtil.LongParam(id);

        replyService.deleteReply(id);
        return new Result(ResultCode.SUCCESS,null);
    }




    @Resource
    private ReplyService replyService;


}
